package com.iris.live.services.report.sources;

import com.iris.live.services.common.NumberUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.SalesRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.RequestBase;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import com.iris.live.services.services.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 行政拆分，订单查看导出
 */
@Component("ExportStockSource")
public class ExportStockSource  implements IExcelReportSource {
    @Autowired
    private SalesRepository repository;

    @Autowired
    private SalesService service;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        Map params = (Map)context.get("param");
        if("01".equals(params.get("car_state"))){
            sheets[0] = new Sheet("经销商 销售行政已归档交车导入模版", "经销商 销售行政已归档交车导入模版", true);  // 是因为销售行政拆分需求不兼容 刘伟龙修改 违背开闭原则  所做修改
        }else{
        sheets[0] = new Sheet("销售行政标准导出模板", "销售行政标准导出模板", true);
        }

        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String dealer = (String) context.get("dealer");
        Map params = (Map)context.get("param");
        String[] columns = (String[]) context.get("columns"), columnPair;
        //keys存放数据Key， titles存放列表头
        String[] keys = new String[columns.length], titles = new String[columns.length + 1];

        int index;
        List<Row> rows = new ArrayList<>();
        for(index = 0; index < 5; index++) {
            rows.add(new Row());
        }

        //标题输出
        titles[0] = "NO.";
        for(index = 0; index < columns.length; index++){
            columnPair = columns[index].split(",");
            keys[index] = columnPair[1];
            titles[index+1] = columnPair[0];
        }
        rows.add(new Row(getTitleCells(titles)));

        //数据输出
        index = 0;
        RequestBase request = (RequestBase) context.get("request");
        PageData pageData = repository.getStoreInfoes(dealer,params, request);
        if(pageData != null) {
            List datas = pageData.getData();
//            service.calculateGp(datas); // 计算GP
            if (datas != null) {
                for (Object o : datas) {
                    rows.add(new Row(getDataCells(index++, keys, o)));
                }
            }
        }

        return rows;
    }

    private  Cell[] getTitleCells(String[] titles){
        Cell[] cells = new Cell[titles.length];
        for(int index = 0; index < titles.length; index++){
            cells[index] = new Cell(titles[index], index );
        }

        return cells;
    }

    private Cell[] getDataCells(int rowIndex, String[] columns, Object o) {
        Map map = (Map) o;
        Cell[] cells = new Cell[columns.length + 1];
        cells[0] = new Cell(rowIndex + "", 0);
        Object value = null;
        for(int index = 0; index < columns.length; index++){
            value = map.get(columns[index]);

            if(StringUtils.isNullOrBlank(value)){
                cells[index + 1] = new Cell("", index + 1);
            }else if(NumberUtils.isDouble(map.get(columns[index]))){ //Double型小数点后保留两位, Added by Tony, 2017-06-27
                cells[index + 1] = new Cell(NumberUtils.format(Double.valueOf(map.get(columns[index]).toString()), 2), index + 1);
            }else {
                cells[index + 1] = new Cell(map.get(columns[index]) == null ? "" : map.get(columns[index]).toString(), index + 1);
            }
        }

        return cells;
    }
}